package com.mini.grpc.examples.server;

import com.mini.grpc.server.Server;
import com.mini.grpc.server.ServerBuilder;
import com.mini.grpc.server.ServerServiceDefinition;
import com.mini.grpc.server.ServerCallHandler;
import com.mini.grpc.server.ServerCall;
import com.mini.grpc.server.BindableService;
import com.mini.grpc.common.MethodDescriptor;
import com.mini.grpc.common.Metadata;
import com.mini.grpc.common.Status;
import com.mini.grpc.serialization.json.JsonMarshaller;

import java.util.logging.Logger;

/**
 * 简单的服务端使用示例
 * 展示如何使用Mini-gRPC服务端组件
 * 
 * @author Mini-gRPC
 */
public class SimpleServerExample {
    
    private static final Logger logger = Logger.getLogger(SimpleServerExample.class.getName());
    
    public static void main(String[] args) throws Exception {
        // 创建服务端
        Server server = ServerBuilder.forPort(9090)
                .addService(new GreeterService())
                .build();
        
        // 启动服务端
        server.start();
        logger.info("Server started on port " + server.getPort());
        
        // 等待终止
        server.awaitTermination();
    }
    
    /**
     * 示例服务实现
     */
    static class GreeterService implements BindableService {
        
        // 定义方法描述符
        private static final MethodDescriptor<HelloRequest, HelloResponse> SAY_HELLO_METHOD =
                MethodDescriptor.<HelloRequest, HelloResponse>newBuilder()
                        .setMethodType(MethodDescriptor.MethodType.UNARY)
                        .setFullMethodName("helloworld.Greeter/SayHello")
                        .setRequestMarshaller(new JsonMarshaller<>(HelloRequest.class))
                        .setResponseMarshaller(new JsonMarshaller<>(HelloResponse.class))
                        .build();
        
        @Override
        public ServerServiceDefinition bindService() {
            return ServerServiceDefinition.builder("helloworld.Greeter")
                    .addMethod(SAY_HELLO_METHOD, new SayHelloHandler())
                    .build();
        }
        
        /**
         * SayHello方法处理器
         */
        private static class SayHelloHandler implements ServerCallHandler<HelloRequest, HelloResponse> {
            
            @Override
            public ServerCall.Listener<HelloRequest> startCall(ServerCall<HelloRequest, HelloResponse> call, 
                                                               Metadata headers) {
                return new ServerCall.Listener<HelloRequest>() {
                    @Override
                    public void onMessage(HelloRequest request) {
                        // 处理请求
                        HelloResponse response = new HelloResponse();
                        response.setMessage("Hello " + request.getName() + "!");
                        
                        // 发送响应
                        call.sendMessage(response);
                        call.close(Status.OK, new Metadata());
                    }
                    
                    @Override
                    public void onHalfClose() {
                        // 客户端完成发送
                    }
                    
                    @Override
                    public void onCancel() {
                        logger.info("Call cancelled");
                    }
                };
            }
        }
    }
    
    /**
     * 请求消息类
     */
    static class HelloRequest {
        private String name;
        
        public String getName() {
            return name;
        }
        
        public void setName(String name) {
            this.name = name;
        }
    }
    
    /**
     * 响应消息类
     */
    static class HelloResponse {
        private String message;
        
        public String getMessage() {
            return message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
    }
} 